const express = require('express');
const router = express.Router();
const sqlFn = require('../mysql');

/**
 * @swagger
 * tags:
 *   name: Location
 *   description: 省市区管理接口
 */

/**
 * @swagger
 * /location/province:
 *   get:
 *     summary: 获取所有省份
 *     description: 返回所有省份数据。
 *     tags: [Location]
 *     responses:
 *       200:
 *         description: 成功返回省份列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 records:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         example: 1
 *                       name:
 *                         type: string
 *                         example: "北京市"
 */
router.get('/province',function(req,res,next){
    const sql='select * from province'
    sqlFn(sql,[],function(err,data){
        if(err){
            next(err)
            return
        }
        res.send({code:'00000',records:data})
    })
})

/**
 * @swagger
 * /location/city:
 *   get:
 *     summary: 获取指定省份的城市列表
 *     description: 根据省份 ID 获取对应的城市列表。
 *     tags: [Location]
 *     parameters:
 *       - name: pid
 *         in: query
 *         description: 省份 ID
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功返回城市列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: string
 *                   example: '00000'
 *                 records:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         example: 1
 *                       name:
 *                         type: string
 *                         example: "北京市"
 */
router.get('/city',function(req,res,next){
    const sql='select * from city where `pid`=?'
    sqlFn(sql,[req.query.pid],function(err,data){
        if(err){
            next(err)
            return
        }
        res.send({code:'00000',records:data})
    })
})

/**
 * @swagger
 * /location/region:
 *   get:
 *     summary: 获取指定城市的区县列表
 *     description: 根据城市 ID 获取对应的区县列表。
 *     tags: [Location]
 *     parameters:
 *       - name: pid
 *         in: query
 *         description: 城市 ID
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功返回区县列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: string
 *                   example: '00000'
 *                 records:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         example: 1
 *                       name:
 *                         type: string
 *                         example: "朝阳区"
 */
router.get('/region',function(req,res,next){
    const sql='select * from region where `pid`=?'
    sqlFn(sql,[req.query.pid],function(err,data){
        if(err){
            next(err)
            return
        }
        res.send({code:200,records:data})
    })
})

module.exports = router;

